<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label-width="auto"
        :label="$t('attribute')"
        prop="publicattributeName"
      >
        <el-input
          v-model="queryParams.publicattributeName"
          :placeholder="$t('pleaseEnterPublicAttributeName')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:type:add']"
          >{{ $t("add") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
          >{{ $t("unfold") }}/{{ $t("packUp") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="typeList"
      row-key="publicattributeId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column :label="$t('attribute')" prop="remark" />

      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        :label="$t('type')"
      >
        <template slot-scope="scope">
          <router-link
            :to="
              '/system/publicattributeData/index/' + scope.row.publicattributeId
            "
            class="link-type"
          >
            <span>{{ scope.row.publicattributeType }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('controls')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:type:edit']"
            >{{ $t("edit") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:type:add']"
            >{{ $t("add") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:type:remove']"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改产品共有属性类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('name')" prop="publicattributeName">
          <el-input
            v-model="form.publicattributeName"
            :placeholder="$t('pleaseEnterTheBasicAttributeName')"
          />
        </el-form-item>

        <el-form-item :label="$t('sort')" prop="sort">
          <el-input
              v-model="form.sort"
              :placeholder="$t('sort')"
              type="number"
          />
        </el-form-item>
        <el-form-item :label="$t('remark')" prop="remark">
          <el-input
            v-model="form.remark"
            :placeholder="$t('pleaseEnterTheContent')"
          />
        </el-form-item>
        <el-form-item
          :label="$t('attributeClass')"
          prop="informtRoleid"
          v-show="this.form.parentId != 0"
        >
          <el-select
            v-model="form.featureId"
            :placeholder="$t('pleaseSelectAttributeClass')"
            style="width: 380px"
          >
            <el-option
              v-for="dict in dict.type.menu_product_feature"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('Parent class')"
          prop="parentId"
          v-show="isAdd"
        >
          <treeselect
            v-model="form.parentId"
            :options="typeOptions"
            :normalizer="normalizer"
            :placeholder="$t('pleaseSelectParentClass')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t("ok") }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
} from "@/api/system/publicattributeType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Type",
  dicts: ["menu_product_feature"],
  components: {
    Treeselect,
  },
  data() {
    return {
      isAdd: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 产品共有属性类型表格数据
      typeList: [],
      // 产品共有属性类型树选项
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        publicattributeName: null,
        publicattributeType: null,
        status: null,
        parentId: null,
        rank: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品共有属性类型列表 */
    getList() {
      this.loading = true;
      listType(this.queryParams).then((response) => {
        this.typeList = this.handleTree(
          response.data,
          "publicattributeId",
          "parentId"
        );
        this.loading = false;
      });
    },
    /** 转换产品共有属性类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.publicattributeId,
        label: node.remark,
        children: node.children,
      };
    },
    /** 查询产品共有属性类型下拉树结构 */
    getTreeselect() {
      listType().then((response) => {
        this.typeOptions = [];
        const data = {
          publicattributeId: 0,
          remark: this.$t("top-node"),
          children: [],
        };
        data.children = this.handleTree(
          response.data,
          "publicattributeId",
          "parentId"
        );
        this.typeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        publicattributeId: null,
        publicattributeName: null,
        publicattributeType: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        parentId: null,
        rank: null,
        featureId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      this.isAdd = true;
      if (row != null && row.publicattributeId) {
        this.form.parentId = row.publicattributeId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = this.$t("addPublic");
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      this.isAdd = false;
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getType(row.publicattributeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t("addPublic");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.publicattributeId != null) {
            updateType(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("modifiedSuccessfully"));
                this.open = false;
              this.getList();

            });
          } else {
            addType(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("newSuccessfullyAdded"));
                this.open = false;
              this.getList();

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm(
          this.$t("areYouSureYouWantToDeleteTheDataItemWithTypeNumber") +
            '"' +
            row.publicattributeId +
            '"'
        )
        .then(function () {
          return delType(row.publicattributeId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("theDeletionIsSuccessful"));
        })
        .catch(() => {});
    },
  },
};
</script>
